package com.df.hitime.common.config;

import com.df.hitime.common.Constants;
import com.df.hitime.common.DateUtils;
import com.df.hitime.common.MyException;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;

/**
 * @Description:
 * @Author: huangwl
 * @CreateDate: 2018/11/30 10:09
 */
@Slf4j
@Aspect
@Component
public class MyAop {

    @Resource
    private Config config;
    @Resource
    HttpServletRequest request;

    @Pointcut("@annotation(com.df.hitime.common.config.MyAuth)")
    public void before() {

    }

    @Before("before()")
    public void checkToken(JoinPoint joinPoint) throws MyException {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        log.info("method:" + methodSignature.getMethod());
        log.info("授权验证开始");
        getHead(request);
        String token = request.getHeader("token");
        String url=request.getHeader("url");
        if (token == null) {
            token = getCookie(request, "token");
        }
        log.info("AOP获取到token:" + token);
        log.info("AOP获取url:"+url);
        if(StringUtils.isEmpty(url)){
            url=config.getServerAddr()+"/home/summit";
        }
        String hashCode=String.valueOf(url.hashCode());
        if (token == null) {
            throw new MyException("暂未授权&"+hashCode);
        }
        HashMap<String, HashMap<String, Object>> sysMap = Constants.tokenMap;
        HashMap<String,Object> urlMap=new HashMap<>();
        urlMap.put(hashCode,url);
        sysMap.put("url",urlMap);
        if (sysMap.get(token) != null) {
            HashMap<String, Object> mm = sysMap.get(token);
            Date date = (Date) mm.get("date");
            if (new Date().getTime() >= date.getTime()) { //超时
                throw new MyException("暂未授权&"+hashCode);
            }
            mm.put("date", DateUtils.addData(1));
            sysMap.put(token, mm);
        } else {
            throw new MyException("暂未授权&"+hashCode);
        }
        log.info("授权验证结束");
    }

    public void getHead(HttpServletRequest request) {
        System.out.print(request.getHeaderNames());
        Enumeration<?> enum1 = request.getHeaderNames();
        while (enum1.hasMoreElements()) {
            String key = (String) enum1.nextElement();
            String value = request.getHeader(key);
            log.info(key + "\t" + value);
        }
    }

    public String getCookie(HttpServletRequest request, String cookieName) {

        Cookie[] cookies = request.getCookies();
        log.info("cookies:" + cookies);
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                if (cookie.getName().equals(cookieName)) {
                    return cookie.getValue();
                }
            }
        }

        return null;
    }
}
